﻿package cocoaas3.flash{

	import jp.progression.casts.*;
	import jp.progression.commands.display.*;
	import jp.progression.commands.lists.*;
	import jp.progression.commands.managers.*;
	import jp.progression.commands.media.*;
	import jp.progression.commands.net.*;
	import jp.progression.commands.tweens.*;
	import jp.progression.commands.*;
	import jp.progression.data.*;
	import jp.progression.events.*;
	import jp.progression.scenes.*;

	import flash.text.*;
	import flash.display.*;
	import flash.geom.*;
	import flash.events.*;

	import jp.dividual.nativeExtensions.*;

	import cocoaas3.*;

	public class UINavigationBar extends UIView {
		//
		//-------------------------------------------------------------------------------------------
		// original variables
		//-------------------------------------------------------------------------------------------
		//

		// An array of navigation items managed by the navigation bar.
		public var items:Vector.<UINavigationItem> = new Vector.<UINavigationItem>();

		// The navigation item at the top of the navigation bar’s stack. (read-only)
		public function get topItem():UINavigationItem{
			return items[items.length-1];
		}


		//
		//-------------------------------------------------------------------------------------------
		// custom variables
		//-------------------------------------------------------------------------------------------
		//
		static public var defaultBackgroundImageClass:Class;
		private var _backgroundContainer_mc:Sprite = new Sprite();
		private var itemContainer_mc:Sprite = new Sprite(); 




		//
		//-------------------------------------------------------------------------------------------
		// original functions
		//-------------------------------------------------------------------------------------------
		//

		public function UINavigationBar(){
			super();
			addChild( _backgroundContainer_mc );
			addChild( itemContainer_mc );

			if( defaultBackgroundImageClass ){
				setBackgroundImage( new defaultBackgroundImageClass() );
			}
		}


		// 背景を変更
		public function setBackgroundImage( mc:Sprite ):void{
			_backgroundContainer_mc.removeChildren();
			_backgroundContainer_mc.addChild( mc );
		}

		// レシーバのスタックにアイテムを追加してナビゲーションバーを更新する。
		public function pushNavigationItem( item:UINavigationItem, animated:Boolean=true ):void{
			trace( "UINavigationBar pushNavigationItem" );
			items.push( item );

			// 古いのを消す
			var old_spr:NavigationBarItemsSprite;
			for( var i:int=0; i<itemContainer_mc.numChildren; i++ ){
				old_spr = itemContainer_mc.getChildAt(i) as NavigationBarItemsSprite;
				old_spr.mode = "push";
				old_spr.animated = animated;
			}
			new RemoveAllChildren( itemContainer_mc ).execute();

			// UINavigationItem から Sprite を作成
			var navItemSprite:NavigationBarItemsSprite = new NavigationBarItemsSprite( item );
			navItemSprite.animated = animated;
			navItemSprite.mode = "push";
			new AddChild( itemContainer_mc, navItemSprite ).execute();

		}

		// レシーバのスタックの最上位のアイテムを削除してナビゲーションバーを更新する。
		public function popNavigationItem( animated:Boolean=true ):UINavigationItem{
			var item:UINavigationItem = items.pop();
			var prevItem:UINavigationItem = items[items.length-1];

			// 古いのを消す
			var old_spr:NavigationBarItemsSprite;
			for( var i:int=0; i<itemContainer_mc.numChildren; i++ ){
				old_spr = itemContainer_mc.getChildAt(i) as NavigationBarItemsSprite;
				old_spr.mode = "pop";
				old_spr.animated = animated;
			}
			new RemoveAllChildren( itemContainer_mc ).execute();

			// UINavigationItem から Sprite を作成
			var navItemSprite:NavigationBarItemsSprite = new NavigationBarItemsSprite( prevItem );
			navItemSprite.animated = animated;
			navItemSprite.mode = "pop";
			new AddChild( itemContainer_mc, navItemSprite ).execute();


			return item;
		}

		// Replaces the navigation items currently managed by the navigation bar with the specified items.
		// ルートに戻るときに使う？
		public function setItems( vec:Vector.<UINavigationItem>, animated:Boolean=true ):void{
			items = vec;

			// 古いのを消す
			var old_spr:NavigationBarItemsSprite;
			for( var i:int=0; i<itemContainer_mc.numChildren; i++ ){
				old_spr = itemContainer_mc.getChildAt(i) as NavigationBarItemsSprite;
				old_spr.mode = "pop";
				old_spr.animated = animated;
			}
			new RemoveAllChildren( itemContainer_mc ).execute();

			var item:UINavigationItem = items[0];

			// UINavigationItem から Sprite を作成
			var navItemSprite:NavigationBarItemsSprite = new NavigationBarItemsSprite( item );
			navItemSprite.animated = animated;
			navItemSprite.mode = "pop";
			new AddChild( itemContainer_mc, navItemSprite ).execute();
		}



		//
		//
		// custom
		//
		//


	}
}



import jp.progression.casts.*;
import jp.progression.commands.display.*;
import jp.progression.commands.lists.*;
import jp.progression.commands.managers.*;
import jp.progression.commands.media.*;
import jp.progression.commands.net.*;
import jp.progression.commands.tweens.*;
import jp.progression.commands.*;
import jp.progression.data.*;
import jp.progression.events.*;
import jp.progression.scenes.*;
import caurina.transitions.Tweener;

import flash.text.*;
import flash.display.*;
import flash.geom.*;
import flash.events.*;

import cocoaas3.*;

class NavigationBarItemsSprite extends CastSprite{

	private var _navItem:UINavigationItem;
	public var animated:Boolean = true;
	public var mode:String = "push";// push or pop
	private var title_txt:TextField;

	public function NavigationBarItemsSprite( navItem:UINavigationItem ){
		trace("NavigationBarItemsSprite constructor");
		this._navItem = navItem;

		title_txt = new TextField();
		title_txt.text = navItem.title;
		this.addChild( title_txt );

		if( navItem.backBarButtonItem ){
			this.addChild( navItem.backBarButtonItem.customView );
		}

		if( navItem.titleView ){
			this.addChild( navItem.titleView );
			navItem.titleView.x = CastDocument.center;
			navItem.titleView.y = 88 / 2;
		}

		if( navItem.rightBarButtonItem ){
			this.addChild( navItem.rightBarButtonItem.customView );
		}

		this.addEventListener( Event.ADDED_TO_STAGE, trace );
	}

	private function onTitleChanged( e:Event ):void{
		trace("NavigationBarItemsSprite onTitleChanged");
		var navItem:UINavigationItem = e.target as UINavigationItem;
		title_txt.text = navItem.title;
	}

	/**
	 * IExecutable オブジェクトが AddChild コマンド、または AddChildAt コマンド経由で表示リストに追加された場合に送出されます。
	 * このイベント処理の実行中には、ExecutorObject を使用した非同期処理が行えます。
	 */
	override protected function atCastAdded():void {
		self.addEventListener( "onTitleChange", onTitleChanged );
		if( animated ){
			switch( mode ){
				case "push":
					this.x = stage.stageWidth/2;
					break;
				case "pop":
					this.x = 0 - stage.stageWidth/2;
					break;
			}
			this.alpha = 0;
			Tweener.addTween( this, {x:0, "_autoAlpha":1, time:0.33, transition:"easeInOutSine"} );
		}
	}

	/**
	 * IExecutable オブジェクトが RemoveChild コマンド、または RemoveAllChild コマンド経由で表示リストから削除された場合に送出されます。
	 * このイベント処理の実行中には、ExecutorObject を使用した非同期処理が行えます。
	 */
	override protected function atCastRemoved():void {
		self.removeEventListener( "onTitleChange", onTitleChanged );
		if( animated ){
			var tx:int;
			switch( mode ){
				case "push":
					tx = 0 - stage.stageWidth/2;
					break;
				case "pop":
					tx = stage.stageWidth/2;
					break;
			}
			addCommand( new DoTweener( this, {x:tx, "_autoAlpha":0, time:0.33, transition:"easeInOutSine"} ) );
		}
	}


}